package jjn.round1;

import java.util.Objects;
import java.util.Stack;

/**
 * @author Jjn
 * @since 2023/4/26 13:56
 */
public class Offer30_MinStack {
    public static class MinStack {
        
        private final Stack<Integer> dataStack;
        private final Stack<Integer> minStack;
        
        /**
         * initialize your data structure here.
         */
        public MinStack() {
            this.dataStack = new Stack<>();
            this.minStack = new Stack<>();
        }
        
        public void push(int x) {
            if (!minStack.isEmpty()) {
                Integer peek = minStack.peek();
                if (peek >= x) {
                    minStack.push(x);
                }
            } else {
                minStack.push(x);
            }
            dataStack.push(x);
        }
        
        public void pop() {
            Integer pop = dataStack.pop();
            if (Objects.equals(pop, minStack.peek())) {
                minStack.pop();
            }
        }
        
        public int top() {
            return dataStack.peek();
        }
        
        public int min() {
            return minStack.peek();
        }
    }
}
